/*********************************************************************
	\file :           os_sw_layers\bios\mcu\Mcu.c 
	\brief:           PLL initialization prototypes and functions definitions.
	\author:          Manuel Sandoval
	\version:         1.0
	\date:            Sat, 01, March 2014
*********************************************************************/


/******************************* Includes**********************************/

#include "Mcu.h"


/******************************* Defines **********************************/


/******************************* Enumerations******************************/


/******************************* Structures *******************************/


/******************************* Global Variables **********************************/


/******************************* Prototypes ********************************/

/*!< Turn PLL ON. */
static void vfnPLL_On(void);

/*!< Turn PLL OFF. */
static void vfnPLL_Off(void);


/******************************* Functions ********************************/


/***************************************************************************
*
*   FUNCTION NAME: vfnPLL_On
*
***************************************************************************/
/**
* @brief Set up the PLL On initialization.
*
* This function allows the application to set the mcu initialization.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
static void vfnPLL_On(void)
{
	/*!< PLL enabled */
    PLLCTL_PLLON = 1u;                   
              
    /*!< Wait until the PLL is within the desired frequency */
    while(!CRGFLG_LOCK)
    {

    }

    /*!< Clear IPLL Lock Interrupt Flag */
    CRGFLG_LOCKIF = 1u;

    /*!< Interrupt will be requested whenever LOCKIF(IPLL Lock Interrupt Flag)is set */
    CRGINT_LOCKIE = 1u;

}


/***************************************************************************
*
*   FUNCTION NAME: vfnPLL_Off
*
***************************************************************************/
/**
* @brief Set up the PLL Off initialization.
*
* This function allows the application to set the mcu initialization.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
static void vfnPLL_Off(void)
{     
	/*!< Disable the PLL */
    PLLCTL_PLLON = 0u;     
}


/***************************************************************************
*
*   FUNCTION NAME: Mcu_Init
*
***************************************************************************/
/**
* @brief Set up de microcontroller initialization.
*
* This function allows the application to set the mcu initialization.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
void Mcu_Init( void )
{
   volatile u32 u32temp = 0;

   /*!< Disable PLL */
    vfnPLL_Off();  

    /*!< Set PLL synthesizer register */
    SYNR_SYNDIV = CNF_SYNDIV_VALUE;    
    
    /*!< Set PLL divider register */
    REFDV_REFDIV = CNF_REFDIV_VALUE; 
    
    /*!< Configure PLL filter for optimal stability and lock time */
    REFDV_REFFRQ = CNF_REFFRQ_VALUE;
            
    /*!< Set Postdiv value,  */
    POSTDIV_POSTDIV = CNF_POSTDIV_VALUE; 
    
    /*!< Configure VCO gain for optimal stability and lock time */
    SYNR_VCOFRQ = CNF_VCOFRQ_VALUE;   
          
    /*!< Clock Monitor Enable. This is required to enter Self Clock Mode */
    PLLCTL_CME = 1u;   
                      
    /*!< Self Clock Mode Enable, If CME & SCME are both asserted and the Clock Monitor
    in the oscillator block detects a loss of clock, Self Clock Mode will be entred */
    PLLCTL_SCME = 1u;          
              
    /*!< Clear Self Clock Mode Interrupt Flag */
    CRGFLG_SCMIF = 1u;          
             
    /*!< Interrupt will be requested whenever SCMIF(Self Clock Mode Interrupt Flag) is set */
    CRGINT_SCMIE = 1u;        
                
    /*!< Enable PLL and wait for locking */
    vfnPLL_On();

}



/***************************************************************************
*
*   FUNCTION NAME: Mcu_DistributePllClock
*
***************************************************************************/
/**
* @brief Couples the PLL output to the entire system as clock source..
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/

void Mcu_DistributePllClock( void ) 
{
  #ifdef CNF_PLL_AS_CLOCK_SOURCE

	/*!< Select PLL as clock source */
    CLKSEL_PLLSEL = 1u;

  #endif
}


/** @} *//* End of MCU Group */


/*********************************************************************
	File	: os_sw_layers\bios\mcu\Mcu.c 
*********************************************************************/
